package com.bbs.controller;

import com.bbs.domain.ResultInfo;
import com.bbs.entity.Post;
import com.bbs.entity.Reply;
import com.bbs.entity.Topic;
import com.bbs.entity.User;
import com.bbs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PostController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private PostService postService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    //去发帖的页面
    @RequestMapping("/toPublish")
    public String toPublish(Model model, HttpSession session){

        if(session.getAttribute("uid") == null){
            return "login";
        }
        List<Topic> topicList = topicService.listTopic();
        model.addAttribute("topicList",topicList);
        return "publish";
    }

    @RequestMapping("/publishPost")
    @ResponseBody
    public ResultInfo publishPost(@RequestBody Post post){
        System.out.println(post);
        int i = postService.insertPost(post);
        System.err.println(i);
        ResultInfo resultInfo = new ResultInfo( );
        resultInfo.setMsg("发布成功");
        resultInfo.setCode("0");
        return resultInfo;
    }


    //帖子详情
    @RequestMapping("/toPost")
    public String toPost(int pid,Model model){
        Post post = postService.findById(pid);

        //回复列表
        List<Reply> replyList = replyService.findAllReply(pid);
        model.addAttribute("post",post);
        model.addAttribute("replyList",replyList);
        return "post";
    }

    //楼中楼评论
    @RequestMapping("/comment")
    public String comment(int rid,String content, HttpSession session){
        int uid=(int)session.getAttribute("uid");
        commentService.insertComment(rid,content,uid);
        return "forward:/toPost";
    }

    //评论帖子
    @RequestMapping("/reply")
    @ResponseBody
    public ResultInfo reply(@RequestBody Reply reply,HttpSession session){
        System.err.println("展示层"+reply);
        ResultInfo resultInfo = new ResultInfo();
        if (session.getAttribute("uid")!=null){
            int uid =(int) session.getAttribute("uid");
            reply.setUid(uid);
            int flag = replyService.insertReply(reply);//插入评论
            if (flag == 1){
                resultInfo.setCode("0");
                resultInfo.setMsg("评论成功");
                replyService.updatePostReplyTime(reply);//评论成功后修改回复时间
                replyService.updatePostReplyCount(reply);//评论成功后修改回贴数量
                userService.updateUserPostCount(reply);//评论成功后更新用户发帖数量
            }else{
                resultInfo.setCode("1");
                resultInfo.setMsg("评论失败");
            }
        }
        return resultInfo;
    }

}
